//
//  MK_HorizonShowView.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/24.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_HorizonShowView.h"
#import "MK_HorizonShow_Layout.h"
#import "MK_Horizon_Cell.h"
#import <SDWebImage/UIImageView+WebCache.h>

#define MK_HorizonShowView_CellName @"MK_HorizonShowView_CellName"

@interface MK_HorizonShowView () <UICollectionViewDelegate,UICollectionViewDataSource>

///滚动控件
@property(nonatomic,weak)UICollectionView* contentCol;
///滚动控件布局对象
@property(nonatomic,weak)MK_HorizonShow_Layout* layout;

///行数
@property(nonatomic,assign)NSInteger rowNum;
///列数
@property(nonatomic,assign)NSInteger colNum;
///url数组
@property(nonatomic,strong)NSArray<NSString*>* urlArr;
///标题数组
@property(nonatomic,strong)NSArray<NSString*>* titleArr;

@end

@implementation MK_HorizonShowView

+(instancetype)MK_HorizonShowViewWithColNum:(NSInteger)colNum andRowNum:(NSInteger)rowNum andImageUrlArr:(NSArray<NSString*>*)urlStrArr andTitleArr:(NSArray<NSString*>*)titleArr {
    MK_HorizonShowView* res = [MK_HorizonShowView new];
    res.rowNum = rowNum;
    res.colNum = colNum;
    res.urlArr = urlStrArr;
    res.titleArr = titleArr;
    return res;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUpUI];
    }
    return self;
}

///初始化
-(void)setUpUI{
    
    MK_HorizonShow_Layout* layout = [MK_HorizonShow_Layout new];
    _layout = layout;
    layout.colNum = _colNum;
    layout.rowNum = _rowNum;
    layout.cellNum = _urlArr.count;
    UICollectionView* col = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _contentCol = col;
    col.delegate = self;
    col.dataSource = self;
    col.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [col registerNib:[UINib nibWithNibName:@"MK_Horizon_Cell" bundle:nil] forCellWithReuseIdentifier:MK_HorizonShowView_CellName];
    [self addSubview:col];
    [col mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
}

#pragma mark 代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return (_colNum*_rowNum)-_urlArr.count%(_colNum*_rowNum)+_urlArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MK_Horizon_Cell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:MK_HorizonShowView_CellName forIndexPath:indexPath];
    if (indexPath.row<_urlArr.count) {
        cell.titleLa.text = _titleArr[indexPath.item];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:_urlArr[indexPath.item]]];
    }else{
        cell.titleLa.text = nil;
        cell.imageV.image = nil;
    }

    return cell;
}

-(void)reloadDataWithColNum:(NSInteger)colNum andRowNum:(NSInteger)rowNum andImageUrlArr:(NSArray<NSString*>*)urlStrArr andTitleArr:(NSArray<NSString*>*)titleArr {
    self.rowNum = rowNum;
    self.colNum = colNum;
    self.urlArr = urlStrArr;
    self.titleArr = titleArr;
    _layout.colNum = _colNum;
    _layout.rowNum = _rowNum;
    _layout.cellNum = _urlArr.count;
    [_contentCol reloadData];
}

@end
